package com.evan.kdl.test.day01;

import cn.hutool.core.util.ObjectUtil;
import com.evan.kdl.util.CommonUtils;

import java.util.concurrent.CompletableFuture;

/**
 * @author huangzhongliang
 * 需求：对回调链中的一次异常进行恢复处理
 * @time 2024/01/05 16:57
 */
public class HandleExceptionDemo2 {
    public static void main(String[] args) {
        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            //int r = 1 / 0;
            return "result1";
        }).handle((ret, ex) -> {
            if (ObjectUtil.isNotEmpty(ex)) {
                System.out.println("我们得到异常：" + ex.getMessage());
                return "unknow1";
            }
            return ret;
        }).thenApply(result -> {
            String str = null;
            int length = str.length();
            return result + " result2";
        }).handle((ret, ex) -> {
            if (ObjectUtil.isNotEmpty(ex)) {
                System.out.println("我们得到异常：" + ex.getMessage());
                return "unknow2";
            }
            return ret;
        }).thenApply(result -> {
            return result + " result3";
        });

        String finalResult = future.join();
        CommonUtils.printThreadLog(finalResult);
    }
}
